package com.niuma.servlet;

import com.niuma.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 一颗蛋50斤
 * 退出操作
 */
public class LogOutServlet extends HttpServlet {
    /**
     * 用户点击退出操作后执行的方法
     * 我们会移除 Session 然后退回到登陆页面
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 Session
        Object userSession = req.getSession().getAttribute(Constants.USER_SESSION);
        // 如果 Session 不为空则将 Session 移除
        if (userSession != null) {
            // 移除 Session
            req.getSession().removeAttribute(Constants.USER_SESSION);
        }
        // 重定向到登陆页面
        resp.sendRedirect("login.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
